/**
 * 
 */
package praktikumid.w09.p3;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * @author Aleksandr
 *
 */
public class Drawing extends JFrame implements MouseListener, MouseMotionListener {
	protected JLabel coordinates;
	protected Point start, end;
	protected JPanel middle;
	
	public Drawing() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("Drawing Demo");
		setSize(300,200);
		setVisible(true);
		
		start = null;
		end = null;		
		
		/* draw user interface */
		DrawUI();
	}
	
	protected void DrawUI() {
		Container pane = getContentPane();
		pane.setLayout(new BorderLayout());
		
	    middle = new JPanel(){
	        @Override
	        public void paintComponent(Graphics g)
	        {
	            super.paintComponent(g);
				if (start != null && end != null) {
					g.drawLine(start.x, start.y, end.x, end.y);
				}
	        }

	    };
	    
		middle.addMouseListener(this);
		middle.addMouseMotionListener(this);
		
		JPanel lower = new JPanel();
		lower.setBackground(Color.LIGHT_GRAY);
		lower.setLayout(new BoxLayout(lower,BoxLayout.X_AXIS));
		
		coordinates = new JLabel(" ");
		lower.add(coordinates);
		
		pane.add(lower, BorderLayout.PAGE_END);
		pane.add(middle, BorderLayout.CENTER);
	}
	
	/* mouse moved event handler */
	public void mouseMoved(MouseEvent e) {
		Point pt = e.getPoint();
		coordinates.setText("Coordinates X: " + pt.x + " Y: " + pt.y);
	}
	
	/* mouse dragged event handler */
	public void mouseDragged(MouseEvent e){
		Point pt = e.getPoint();
		coordinates.setText("Coordinates X: " + pt.x + " Y: " + pt.y);
	}

	public void mouseClicked(MouseEvent e) {}
	
	public void mousePressed(MouseEvent e) {
		start = e.getPoint();
	}
	
	public void mouseReleased(MouseEvent e) {
		end = e.getPoint();
		middle.repaint();
	}
	
	public void mouseExited(MouseEvent e) {}
	
	public void mouseEntered(MouseEvent e) {}
	
	/**
	 * @param args arguments
	 */
	public static void main(String[] args) {
	
		/*
		 * basic initialisation
		 */
		Drawing d = new Drawing();
	}

}
